package com.legstar.talend.xsd2xml;

import java.io.File;
import java.util.Properties;

import com.legstar.codegen.CodeGenMakeException;
import com.legstar.codegen.models.AbstractAntBuildModel;

/**
 * This class gathers execution parameters for the COBOL to TOS Schema utility.
 * <p/>
 * The class is also capable of generating a fully configured ANT script to run
 * the ant version of the utility with the current parameter set.
 * 
 */
public class Cob2TosSchemaModel extends AbstractAntBuildModel {

    /** This generator name. */
    public static final String S2C_GENERATOR_NAME = "LegStar COBOL to TOS Schema generator";

    /** This velocity template. */
    public static final String S2C_VELOCITY_MACRO_NAME = "vlc/build-cob2tos-xml.vm";

    /* ====================================================================== */
    /* Following are key identifiers for this model persistence. = */
    /* ====================================================================== */

    /** File or folder where the COBOL-annotated XML schema are available. */
    public static final String INPUT_COBXSD_FILE = "inputCobXsd";

    /** TOS schema target folder. */
    public static final String TARGET_TOS_FOLDER = "targetTosFolder";

    /* ====================================================================== */
    /* Following are this class fields that are persistent. = */
    /* ====================================================================== */

    /** A File or folder where the COBOL-annotated XML schema are available. */
    private File _inputCobXsd;

    /** The target folder where TOS schemas will be created. */
    private File _targetTosFolder;

    /* ====================================================================== */
    /* Following are other class fields and methods. = */
    /* ====================================================================== */

    /**
     * A no-Arg constructor.
     */
    public Cob2TosSchemaModel() {
        super();
    }

    /**
     * Construct from a properties file.
     * 
     * @param props the property file
     */
    public Cob2TosSchemaModel(final Properties props) {
        super(props);
        setInputCobXsd(getFile(props, INPUT_COBXSD_FILE, null));
        setTargetTosFolder(getFile(props, TARGET_TOS_FOLDER, null));
    }

    /**
     * Creates an ant build script file ready for COBOL generation.
     * 
     * @param scriptFile the script file that must be created
     * @throws CodeGenMakeException if generation fails
     */
    public void generateBuild(File scriptFile) throws CodeGenMakeException {
        generateBuild(S2C_GENERATOR_NAME, S2C_VELOCITY_MACRO_NAME, scriptFile);
    }

    /**
     * The input COBOL-annotated XML schema file or folder.
     * 
     * @return the input COBOL-annotated XML schema file or folder
     */
    public File getInputCobXsd() {
        return _inputCobXsd;
    }

    /**
     * The input COBOL-annotated XML schema file or folder.
     * 
     * @param cobXsd the input COBOL-annotated XML schema file or folder to set
     */
    public void setInputCobXsd(final File cobXsd) {
        _inputCobXsd = cobXsd;
    }

    /**
     * @return the TOS schema target folder
     */
    public File getTargetTosFolder() {
        return _targetTosFolder;
    }

    /**
     * @param targetTosFolder the target TOS schema folder
     */
    public void setTargetTosFolder(final File targetTosFolder) {
        _targetTosFolder = targetTosFolder;
    }

    /**
     * @return a properties file holding the values of this object fields
     */
    public Properties toProperties() {
        Properties props = super.toProperties();
        putFile(props, INPUT_COBXSD_FILE, getInputCobXsd());
        putFile(props, TARGET_TOS_FOLDER, getTargetTosFolder());
        return props;
    }

}
